package com.csj.mvvm.xml

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.SeekBar
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import androidx.databinding.BindingMethod
import androidx.databinding.BindingMethods
import androidx.lifecycle.MutableLiveData
import com.csj.mvvm.App
import com.csj.mvvm.R

data class Theme(
    val content: Int, val background: Int, val modeType: Int  //1-Day 2-Night
)

object Themes {
    val Day = Theme(
        ContextCompat.getColor(App.instance, R.color.theme_black),
        ContextCompat.getColor(App.instance, R.color.theme_white),
        1
    )
    val Night = Theme(
        ContextCompat.getColor(App.instance, R.color.theme_white),
        ContextCompat.getColor(App.instance, R.color.theme_black),
        2
    )

    val Other = Theme(Color.RED, Color.GRAY, 0)
}


object AppTheme {
    val background = MutableLiveData<ColorStateList>()
    val content = MutableLiveData<ColorStateList>()
    var modeType = 1
    //android:background="@{AppTheme.INSTANCE.modeType==1 ? @drawable/rounded_corners : @drawable/rounded_corners_red}"

    init {
        update(Themes.Day)
    }

    fun update(theme: Theme) {
        background.value = ColorStateList.valueOf(theme.background)
        content.value = ColorStateList.valueOf(theme.content)
        modeType = theme.modeType
    }
}

@SuppressLint("RestrictedApi")
@BindingMethods(
    //示例ImageView -> tint setImageTintList是默认的
    BindingMethod(type = ImageView::class, attribute = "tint", method = "setImageTintList")
)
object ThemeAdapter {

    @BindingAdapter("background")
    @JvmStatic
    fun adaptBackground(view: View, value: ColorStateList?) {
        view.setBackgroundColor(Color.WHITE)
        view.backgroundTintList = value
    }

    @BindingAdapter("drawableTint")
    @JvmStatic
    fun adaptDrawableTint(view: View, value: ColorStateList?) {
        if (view is AppCompatTextView) {
            view.supportCompoundDrawablesTintList = value
        } else if (view is ImageView) {
            view.imageTintList = value
//            view.backgroundTintList = value
        } else if (view is RadioButton) {
            view.buttonTintList = value
        }
    }

    @BindingAdapter("android:progressBackgroundTint")
    @JvmStatic
    fun adaptProgressBackgroundTint(view: SeekBar, value: ColorStateList?) {
        view.progressBackgroundTintList = value
    }

}



